home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / programs_-_include / LINUX / I2C.H < prev    next >
C/C++ Source or Header  |  1999-09-17  |  6KB  |  183 lines

  1. #ifndef I2C_H
  2. #define I2C_H
  3.  
  4. /*
  5.  * linux i2c interface.  Works a little bit like the scsi subsystem.
  6.  * There are:
  7.  *
  8.  *     i2c          the basic control module        (like scsi_mod)
  9.  *     bus driver   a driver with a i2c bus         (hostadapter driver)
  10.  *     chip driver  a driver for a chip connected
  11.  *                  to a i2c bus                    (cdrom/hd driver)
  12.  *
  13.  * A device will be attached to one bus and one chip driver.  Every chip
  14.  * driver gets a unique ID.
  15.  *
  16.  * A chip driver can provide a ioctl-like callback for the
  17.  * communication with other parts of the kernel (not every i2c chip is
  18.  * useful without other devices, a TV card tuner for example). 
  19.  *
  20.  * "i2c internal" parts of the structs: only the i2c module is allowed to
  21.  * write to them, for others they are read-only.
  22.  *
  23.  */
  24.  
  25. #define I2C_BUS_MAX       4    /* max # of bus drivers  */
  26. #define I2C_DRIVER_MAX    8    /* max # of chip drivers */
  27. #define I2C_DEVICE_MAX    8    /* max # if devices per bus/driver */
  28.  
  29. struct i2c_bus;
  30. struct i2c_driver;
  31. struct i2c_device;
  32.  
  33. #define I2C_DRIVERID_MSP3400     1
  34. #define I2C_DRIVERID_TUNER       2
  35. #define I2C_DRIVERID_VIDEOTEXT     3
  36.  
  37. #define I2C_BUSID_BT848        1    /* I2C bus on a BT848 */
  38.  
  39. /*
  40.  * struct for a driver for a i2c chip (tuner, soundprocessor,
  41.  * videotext, ... ).
  42.  *
  43.  * a driver will register within the i2c module.  The i2c module will
  44.  * callback the driver (i2c_attach) for every device it finds on a i2c
  45.  * bus at the specified address.  If the driver decides to "accept"
  46.  * the, device, it must return a struct i2c_device, and NULL
  47.  * otherwise.
  48.  *
  49.  * i2c_detach = i2c_attach ** -1
  50.  * 
  51.  * i2c_command will be used to pass commands to the driver in a
  52.  * ioctl-line manner.
  53.  *
  54.  */
  55.  
  56. struct i2c_driver 
  57. {
  58.     char           name[32];         /* some useful label         */
  59.     int            id;               /* device type ID            */
  60.     unsigned char  addr_l, addr_h;   /* address range of the chip */
  61.  
  62.     int (*attach)(struct i2c_device *device);
  63.     int (*detach)(struct i2c_device *device);
  64.     int (*command)(struct i2c_device *device,unsigned int cmd, void *arg);
  65.  
  66.     /* i2c internal */
  67.     struct i2c_device   *devices[I2C_DEVICE_MAX];
  68.     int                 devcount;
  69. };
  70.  
  71.  
  72. /*
  73.  * this holds the informations about a i2c bus available in the system.
  74.  * 
  75.  * a chip with a i2c bus interface (like bt848) registers the bus within
  76.  * the i2c module. This struct provides functions to access the i2c bus.
  77.  * 
  78.  * One must hold the spinlock to access the i2c bus (XXX: is the irqsave
  79.  * required? Maybe better use a semaphore?). 
  80.  * [-AC-] having a spinlock_irqsave is only needed if we have drivers wishing
  81.  *      to bang their i2c bus from an interrupt.
  82.  * 
  83.  * attach/detach_inform is a callback to inform the bus driver about
  84.  * attached chip drivers.
  85.  *
  86.  */
  87.  
  88. /* needed: unsigned long flags */
  89.  
  90. #if LINUX_VERSION_CODE >= 0x020100
  91. # if 0
  92. #  define LOCK_FLAGS unsigned long flags;
  93. #  define LOCK_I2C_BUS(bus)    spin_lock_irqsave(&(bus->bus_lock),flags);
  94. #  define UNLOCK_I2C_BUS(bus)  spin_unlock_irqrestore(&(bus->bus_lock),flags);
  95. # else
  96. #  define LOCK_FLAGS
  97. #  define LOCK_I2C_BUS(bus)    spin_lock(&(bus->bus_lock));
  98. #  define UNLOCK_I2C_BUS(bus)  spin_unlock(&(bus->bus_lock));
  99. # endif
  100. #else
  101. # define LOCK_FLAGS unsigned long flags;
  102. # define LOCK_I2C_BUS(bus)    { save_flags(flags); cli(); }
  103. # define UNLOCK_I2C_BUS(bus)  { restore_flags(flags);     }
  104. #endif
  105.  
  106. struct i2c_bus 
  107. {
  108.     char  name[32];         /* some useful label */
  109.     int   id;
  110.     void  *data;            /* free for use by the bus driver */
  111.  
  112. #if LINUX_VERSION_CODE >= 0x020100
  113.     spinlock_t bus_lock;
  114. #endif
  115.  
  116.     /* attach/detach inform callbacks */
  117.     void    (*attach_inform)(struct i2c_bus *bus, int id);
  118.     void    (*detach_inform)(struct i2c_bus *bus, int id);
  119.  
  120.     /* Software I2C */
  121.     void    (*i2c_setlines)(struct i2c_bus *bus, int ctrl, int data);
  122.     int     (*i2c_getdataline)(struct i2c_bus *bus);
  123.  
  124.     /* Hardware I2C */
  125.     int     (*i2c_read)(struct i2c_bus *bus, unsigned char addr);
  126.     int     (*i2c_write)(struct i2c_bus *bus, unsigned char addr,
  127.              unsigned char b1, unsigned char b2, int both);
  128.  
  129.     /* internal data for i2c module */
  130.     struct i2c_device   *devices[I2C_DEVICE_MAX];
  131.     int                 devcount;
  132. };
  133.  
  134.  
  135. /*
  136.  *    This holds per-device data for a i2c device
  137.  */
  138.  
  139. struct i2c_device 
  140. {
  141.     char           name[32];         /* some useful label */
  142.     void           *data;            /* free for use by the chip driver */
  143.     unsigned char  addr;             /* chip addr */
  144.  
  145.     /* i2c internal */
  146.     struct i2c_bus     *bus;
  147.     struct i2c_driver  *driver;
  148. };
  149.  
  150.  
  151. /* ------------------------------------------------------------------- */
  152. /* i2c module functions                                                */
  153.  
  154. /* register/unregister a i2c bus */
  155. int i2c_register_bus(struct i2c_bus *bus);
  156. int i2c_unregister_bus(struct i2c_bus *bus);
  157.  
  158. /* register/unregister a chip driver */
  159. int i2c_register_driver(struct i2c_driver *driver);
  160. int i2c_unregister_driver(struct i2c_driver *driver);
  161.  
  162. /* send a command to a chip using the ioctl-like callback interface */
  163. int i2c_control_device(struct i2c_bus *bus, int id,
  164.                unsigned int cmd, void *arg);
  165.  
  166. /* i2c bus access functions */
  167. void    i2c_start(struct i2c_bus *bus);
  168. void    i2c_stop(struct i2c_bus *bus);
  169. void    i2c_one(struct i2c_bus *bus);
  170. void    i2c_zero(struct i2c_bus *bus);
  171. int     i2c_ack(struct i2c_bus *bus);
  172.  
  173. int     i2c_sendbyte(struct i2c_bus *bus,unsigned char data,int wait_for_ack);
  174. unsigned char i2c_readbyte(struct i2c_bus *bus,int last);
  175.  
  176. /* i2c (maybe) hardware functions */
  177. int     i2c_read(struct i2c_bus *bus, unsigned char addr);
  178. int     i2c_write(struct i2c_bus *bus, unsigned char addr,
  179.           unsigned char b1, unsigned char b2, int both);
  180.  
  181. int    i2c_init(void);
  182. #endif /* I2C_H */
  183.